package com.arm.controller.order;

import com.arm.controller.WechatBaseController;
import com.arm.def.Constant;
import com.arm.module.ConsigneeInfo;
import com.arm.module.Order;
import com.arm.module.User;
import com.arm.service.OrderItemService;
import com.arm.service.OrderService;
import com.arm.service.UserService;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import com.arm.tools.result.ResultObj;
import com.arm.tools.result.ReturnCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

//import com.github.pagehelper.PageHelper;
//import com.github.pagehelper.PageInfo;

/**
 * Created by baggio on 2016/12/27.
 */
@RequestMapping("/order")
@RestController
public class OrderController extends WechatBaseController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/orderList")
    public ModelAndView orderList(HttpServletRequest request, HttpServletResponse response) {        // source: 1(来源于网站) | 2(来源于微站)
        ModelAndView mov = new ModelAndView();
        mov.setViewName("weChat/order/orderList");
        if (super.getIsMobile(request)) {

            super.oauth2(request, response);
        }
        return mov;
    }

    @GetMapping(value = "/getOrderList")
    public String getOrderList() {
        ResultObj resultObj = new ResultObj();
        try {
            User user = (User) super.session.getAttribute(Constant.SESSION_KEY_USER_LOGIN);
            if(user==null){
                throw new MyRunTimeExcption("没有找到订单！");
            }
            List<Order> orderList = orderService.getOrderList(new HashMap<String, String>() {{
                put("userId", String.valueOf(user.getTid()));
            }});
            resultObj.setData(orderList);
        } catch (MyRunTimeExcption runTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(runTimeExcption.getMessage());
            runTimeExcption.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultObj.toJson();
    }


    @PostMapping(value = "/createOrder")
    public String createOrder(String productJsons, ConsigneeInfo consigneeInfo) {
        ResultObj resultObj = new ResultObj();
        try {
            Order order = orderService.createOrder(productJsons, (User) super.session.getAttribute(Constant.SESSION_KEY_USER_LOGIN), consigneeInfo);
            resultObj.setData(order);
        } catch (MyRunTimeExcption runTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(runTimeExcption.getMessage());
            runTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        logger.info("resultObj==>" + resultObj.toJson());
        return resultObj.toJson();
    }


    @RequestMapping(value = "/cancelOrderItem", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public String cancelOrderItem(HttpServletRequest request, Long orderItemId) {
        ResultObj resultObj = new ResultObj();
        try {
            orderItemService.cancelOrderItem(orderItemId);
        } catch (MyRunTimeExcption runTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(runTimeExcption.getMessage());
            runTimeExcption.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    @RequestMapping(value = "/cancelOrder")
    @ResponseBody
    public String cancelOrder(Long orderId) {
        ResultObj resultObj = new ResultObj();
        try {
            orderService.cancelOrder(orderId);
        } catch (MyRunTimeExcption runTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(runTimeExcption.getMessage());
            runTimeExcption.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultObj.toJson();
    }


    @RequestMapping(value = "/getOrderById")
    @ResponseBody
    public String getOrderById(HttpServletRequest request, Long orderId) {
        logger.info("【getOrderById===>】" + orderId);
        ResultObj resultObj = new ResultObj();
        try {
            Order order = orderService.getOrderById(orderId);
            resultObj.setData(order);

        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
        }
        logger.info("resultObj.toJson()===>>>>" + resultObj.toJson());
        return resultObj.toJson();
    }

    @RequestMapping(value = "/getOrderList", method = RequestMethod.POST)
    @ResponseBody
    public String getOrderList(Integer page, Integer pageSize) {
        logger.info("【getOrderList】");
        ResultObj resultObj = new ResultObj();
        try {
            if (page == null || page == 0) {
                page = Constant.PAGENUM;
            }
            // 设置每页记录数
            if (pageSize == null || pageSize == 0) {
                pageSize = Constant.PAGESIZENum;
            }
            PageHelper.startPage(page, pageSize);

            List<Order> orderList = orderService.getOrderList(new HashMap<String, String>());
            PageInfo<Order> pageinfo = new PageInfo<Order>(orderList);
            resultObj.setData(pageinfo);

        } catch (MyRunTimeExcption runTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(runTimeExcption.getMessage());
            runTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }


    @RequestMapping(value = "/orderConfirm")
    @ResponseBody
    public String orderConfirm(Long orderId) {
        ResultObj resultObj = new ResultObj();
        try {

            if (!orderService.completeOrder(orderId)) {
                resultObj.setCode(ReturnCode.FAIL.getCode());
                resultObj.setMsg("确认支付失败！");
            }

        } catch (MyRunTimeExcption runTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(runTimeExcption.getMessage());
            runTimeExcption.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
    /**
     * 订单状态更新
     *
     * @param orderId
     * @return
     */
    @PostMapping(value = "/completeOrder")
    public void completeOrder(Long orderId) {
        ResultObj resultObj = new ResultObj();
        try {
            orderService.completeOrder(orderId);
            resultObj.setMsg("操作成功");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        logger.info("resultObj====>>>>" + resultObj.toJson());
    }

}
